import request from './index'

// 分享信息
export interface ShareInfo {
  id: number
  userId: number
  fileId: number
  shareCode: string
  shareName: string
  permissions: number
  password?: string
  expireTime?: string
  visitCount: number
  downloadCount: number
  status: number
  createTime: string
  updateTime: string
}

// 分享创建参数
export interface CreateShareParams {
  fileId: number
  shareName: string
  permissions?: number
  password?: string
  expireTimeStr?: string
}

// 分享列表响应
export interface ShareListResponse {
  total: number
  page: number
  size: number
  content: ShareInfo[]
}

// 分享访问信息
export interface ShareAccessInfo {
  shareId: number
  shareName: string
  permissions: number
  needPassword: boolean
  expireTime?: string
  visitCount: number
  downloadCount: number
  file: {
    id: number
    fileName: string
    fileSize: number
    fileType: string
    isFolder: number
    createTime: string
  }
}

/**
 * 创建分享
 * @param params 创建参数 - 接口路径：POST /api/share/create，参数：fileId,shareName,permissions,password,expireTimeStr
 * @returns 分享信息
 */
export const createShare = (params: CreateShareParams): Promise<ShareInfo> => {
  return request.post('/share/create', null, { params })
}

/**
 * 访问分享
 * @param shareCode 分享码
 * @returns 分享信息
 */
export const getShare = (shareCode: string): Promise<ShareAccessInfo> => {
  return request.get(`/share/${shareCode}`)
}

/**
 * 验证分享密码
 * @param shareCode 分享码 - 接口路径：POST /api/share/{shareCode}/verify，参数：password
 * @param password 密码
 * @returns 验证结果
 */
export const verifySharePassword = (shareCode: string, password: string): Promise<{
  verified: boolean
  file: {
    id: number
    fileName: string
    fileSize: number
    fileType: string
    isFolder: number
  }
}> => {
  return request.post(`/share/${shareCode}/verify`, null, { params: { password } })
}

/**
 * 获取我的分享列表
 * @param params 查询参数
 * @returns 分享列表
 */
export const getMyShareList = (params: {
  page?: number
  size?: number
  orderBy?: string
  orderType?: string
}): Promise<ShareListResponse> => {
  return request.get('/share/my/list', { params })
}

/**
 * 取消分享
 * @param shareId 分享ID
 * @returns 取消结果
 */
export const cancelShare = (shareId: number): Promise<any> => {
  return request.post(`/share/${shareId}/cancel`)
}

/**
 * 更新分享设置
 * @param shareId 分享ID - 接口路径：POST /api/share/{shareId}/update，参数：shareName,permissions,password,expireTimeStr
 * @param params 更新参数
 * @returns 更新结果
 */
export const updateShare = (shareId: number, params: {
  shareName?: string
  permissions?: number
  password?: string
  expireTimeStr?: string
}): Promise<ShareInfo> => {
  return request.post(`/share/${shareId}/update`, null, { params })
}

/**
 * 记录分享下载
 * @param shareCode 分享码
 * @returns 记录结果
 */
export const recordShareDownload = (shareCode: string): Promise<any> => {
  return request.post(`/share/${shareCode}/download`)
}